package b2.widget.scrollmsg
{
	import b2.net.*;
	import b2.player.*;
	import b2.widget.*;
	
	import com.bit101.components.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.utils.*;
	import flash.ui.Mouse;
	
	public class SendMsgPanel extends Panel
	{
		//是否花费喇叭
		private var isExpendloudspeaker:Boolean;
		//
		private var textField:TextField;
		//上次记录时间
		private var lastTime:int = 0;
		//最长发送数
		public static const MAX_CHARS:int = 100;
		//最短间隔
		public static const INTERVAL:int = 1000;
		
		public function SendMsgPanel(isExpendloudspeaker:Boolean)
		{
			this._background.alpha = 0.6;
			this.width = 190;
			this.height = 20;
			var rrect:Sprite = new Sprite();
			rrect.graphics.lineStyle(5, 0xFFFFFF);
			rrect.graphics.drawRoundRect(0, 0, 192, 30, 5, 5);
			rrect.filters = [new DropShadowFilter(1, 45, 0x000000, 1, 2, 2, 1, 1, false)];
			rrect.x = -2;
			rrect.y = -2;
	//		addChild(rrect);
			textField = new TextField();
			textField.width = 160;
			textField.height = 20;
			textField.type = TextFieldType.INPUT;
			addChild(textField);
			Style.fontName = "微软雅黑";
			Style.fontSize = 12;
			var button:PushButton = new PushButton(this, 150, 0, "发送");
			button.width = 40;
			addChild(button);
			button.addEventListener(MouseEvent.CLICK, onMouseClick);
			button.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			button.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			//设定最大长度
			textField.maxChars = 100;
			textField.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			//textField.addEventListener(FocusEvent.FOCUS_IN
		}

		private function onMouseOver(event:Event):void
		{
			Mouse.cursor = "button";
		}

		private function onMouseOut(event:Event):void
		{
			Mouse.cursor = "auto";
		}
		
		private function onMouseClick(event:Event):void
		{
			sendMessage();
		}
		
		private function onKeyDown(event:KeyboardEvent):void
		{
			if (event.keyCode == 13)
			{
				sendMessage();
			}
		}
		
		private function sendMessage():void
		{
			var time:int = getTimer();
			if (!checkTime(time))
				return;
			if (!checkContent())
				return;
			if (isExpendloudspeaker)
			{
				if (checkLoudspeaker())
				{
					expendLoudspeaker();
				}
				else
				{
					var popMessage:Sprite = new PopMessage("小喇叭数量不足", 0xff0000);
					WidgetManager.getInstance().popOut([popMessage], false, true);
					return;
				}
			}
			var name:String = PlayerInfoManager.getInstance().me.name;
			SmartFoxHelper.getInstance().sendPublicMessage("<a href ='event:login'><font color='#0000FF'>" + name + "</font></a>: " + textField.text);
			textField.text = "";
			lastTime = time;
		}
		
		private function checkLoudspeaker():Boolean
		{
			var num:int = getPlayerObjectNumByKey("12_0_0_0");
			return num > 0;
		}
		
		private function getPlayerObjectNumByKey(key:String):int
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var num:int = me.objPackage.objectList[key];
			return num;
		}
		
		//消耗喇叭
		private function expendLoudspeaker():void
		{
			ServerHelper.getInstance().expendVert(0, 12, 1, 0 ,0);
		}
		
		private function checkTime(time:int):Boolean
		{
			return time - lastTime > INTERVAL;
		}
		
		private function checkContent():Boolean
		{
			return textField.text.length > 0;
		}
		
		public function setNeedLoudSpeaker(need:Boolean = true):void
		{
			isExpendloudspeaker = need;
		}
	}
}